package org.nowcoder.leetcode.backTracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Title  : 17. Letter Combinations of a Phone Number
 * Source : https://leetcode.cn/problems/letter-combinations-of-a-phone-number/description/
 * Author : XrazYang
 * Date   : 2023-10-20
 */
public class LeetCode_17 {
    private List<String> results = new ArrayList<>();
    StringBuilder builder = new StringBuilder();

    public List<String> letterCombinations(String digits) {
        char[][] numsToAlpha = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n',
                'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}};
        if (digits.isEmpty()) {
            return results;
        }
        backTracking4(digits, numsToAlpha, 0);
        return results;
    }

    private void backTracking4(String digits, char[][] numsToAlpha, int index) {
        if (index == digits.length()) {
            results.add(builder.toString());
            return;
        }
        char[] alphas = numsToAlpha[digits.charAt(index) - '2'];
        for (char c : alphas) {
            builder.append(c);
            backTracking4(digits, numsToAlpha, index + 1);
            builder.deleteCharAt(builder.length() - 1);
        }
    }
}
